package com.stefanhermann.particlestest;

import android.util.FloatMath;

public class Vec2 {
	// #### Class variables
	public float x, y;
	public static float TO_RADIANS = (1 / 180.0f) * (float) Math.PI;
	public static float TO_DEGREES = (1 / (float) Math.PI) * 180;

	// #### Constructors
	public Vec2(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vec2() {
	}

	// #### Class methods

	// Set vector
	public Vec2 set(float x, float y) {
		this.x = x;
		this.y = y;
		return this;
	}

	public Vec2 set(Vec2 vector) {
		this.x = vector.x;
		this.y = vector.y;
		return this;
	}

	// Add vectors
	public Vec2 add(Vec2 vector) {
		this.x += vector.x;
		this.y += vector.y;
		return this;
	}

	public Vec2 add(float x, float y) {
		this.x += x;
		this.y += y;
		return this;
	}

	// Substract vectors
	public Vec2 sub(Vec2 vector) {
		this.x -= vector.x;
		this.y -= vector.y;
		return this;
	}

	public Vec2 sub(float x, float y) {
		this.x -= x;
		this.y -= y;
		return this;
	}

	// Multiply vector with scalar
	public Vec2 mult(float scalar) {
		this.x *= scalar;
		this.y *= scalar;
		return this;
	}

	// Vector length
	public float len() {
		return FloatMath.sqrt(x * x + y * y);
	}

	// Rotate vector
	public Vec2 rotate(float angle) {
		angle = angle * TO_RADIANS;
		float cos = FloatMath.cos(angle);
		float sin = FloatMath.sin(angle);

		this.x = this.x * cos - this.y * sin;
		this.y = this.y * sin + this.y * cos;
		
		return this;
	}
	
	//Copy vector
	public Vec2 cpy(){
		return new Vec2(this.x,this.y);
	}
}
